package com.example.controller;

import cn.hutool.core.util.ObjectUtil;

import cn.hutool.core.util.StrUtil;

import com.example.common.Result;
import com.example.common.enums.ResultCodeEnum;
import com.example.entity.Account;
import com.example.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 登入登出，修改密码前端操作接口
 **/

@RestController
public class WebController {

    @Autowired
    private PatientsService patientsService;
    @Autowired
    private UpdatePasswordService updatePasswordService;
    @Autowired
    private LoginService loginService;
    @Autowired
    private LogoutService logoutService;

    /**
     * 默认请求接口
     */
    @GetMapping("/")
    public Result hello() {
        return Result.success();
    }

    /**
     * 登录
     */
    @PostMapping("/login")
    public Result login(@RequestBody Account account) {
        Account ac = loginService.handelLogin(account);
        /*switch (account.getRole()) {
            case "ADMIN" -> ac = adminService.login(account);
            case "PATIENT" -> ac = patientsService.login(account);
            case "PHARMACIST" -> ac = pharmacistService.login(account);
            case "DOCTOR" -> ac = doctorService.login(account);
        }*/
        return Result.success(ac);
    }

    /**
     * 患者角色注册接口
     */
    @PostMapping("/register")
    public Result register(@RequestBody Account account) {
        if (StrUtil.isBlank(account.getUsername()) || StrUtil.isBlank(account.getPassword())
                || ObjectUtil.isEmpty(account.getRole())) {
            return Result.error(ResultCodeEnum.PARAM_ERROR);
        }
        if ("PATIENT".equals(account.getRole())) {
            patientsService.register(account);
        } else {
            return Result.error(ResultCodeEnum.PARAM_ERROR);
        }
        return Result.success();
    }

    /**
     * 修改密码
     */
    @PutMapping("/updatePassword")
    public Result updatePassword(@RequestBody Account account) {
        updatePasswordService.updatePassword(account);
        /*if (RoleEnum.ADMIN.name().equals(account.getRole())) {
            adminService.updatePassword(account);
        } else if (RoleEnum.PATIENT.name().equals(account.getRole())) {
            patientsService.updatePassword(account);
        } else if (RoleEnum.DOCTOR.name().equals(account.getRole())) {
            doctorService.updatePassword(account);
        } else if (RoleEnum.PHARMACIST.name().equals(account.getRole())) {
            pharmacistService.updatePassword(account);
        }*/
        return Result.success();
    }

    /**
     * 注销
     */
    @PutMapping("/logout")
    public Result logout(@RequestBody Account account) {
        logoutService.handleLogout(account);//主动使redis中的token失效
        return Result.success();
    }
}
